package com.foreveross.mofang.serviceconsole.application.impl.core;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.commons.lang.StringUtils;
import org.dayatang.querychannel.Page;
import org.dayatang.querychannel.QueryChannelService;
import org.openkoala.koala.auth.core.domain.User;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.mofang.serviceconsole.application.core.ChannelApplication;
import com.foreveross.mofang.serviceconsole.application.dto.ChannelDTO;
import com.foreveross.mofang.serviceconsole.core.Channel;

/**
 * 渠道管理基础设施层实现
 * 
 * @author fb
 *
 */
@Named
@Transactional
public class ChannelApplicationImpl implements ChannelApplication {
   
	@Inject
	private QueryChannelService queryChannel;

	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public ChannelDTO getChannel(Long id) {
		Channel channel = Channel.load(Channel.class, id);
		ChannelDTO channelDTO = new ChannelDTO();
		channelDTO.domain2Dto(channel);
		return channelDTO;
	}
	
	public void saveChannel(ChannelDTO channelDTO, User loginUser) {
		Channel channel = new Channel();
		channelDTO.dto2Domain(channel);
		channel.setUser(loginUser);
		channel.setLastUpdateUser(loginUser);
		channel.save();
	}
	
	public void updateChannel(ChannelDTO channelDTO, User loginUser) {
		Channel channel = Channel.get(Channel.class, channelDTO.getId());
		channelDTO.dto2Domain(channel);
		channel.setLastUpdate(new Date());
		channel.setLastUpdateUser(loginUser);
		channel.update();
	}
	
	public void migration(Long group, Long[] ids) {
		for (Long id : ids) {
			Channel.get(Channel.class, id).migration(group);
		}
	}
	
	public void removeChannel(Long id, User loginUser) {
		this.removeChannels(new Long[] { id }, loginUser);
	}
	
	public void removeChannels(Long[] ids, User loginUser) {
		for (int i = 0; i < ids.length; i++) {
			Channel channel = Channel.load(Channel.class, ids[i]);
			channel.remove(loginUser);
		}
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
	public Page<ChannelDTO> pageQueryChannel(ChannelDTO queryVo, int currentPage, int pageSize) {
		List<ChannelDTO> result = new ArrayList<ChannelDTO>();
		List<Object> conditionVals = new ArrayList<Object>();
	   	StringBuilder jpql = new StringBuilder("select _channel from Channel _channel where _channel.removed=false ");

	   	if (queryVo.getGroup() != null) {
		   	jpql.append("and _channel.group.id = ?");
		   	conditionVals.add(queryVo.getGroup());
	   	}
	
	   	if (StringUtils.isNotBlank(queryVo.getSn())) {
	   		jpql.append(" and _channel.sn like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getSn()));
	   	}		
	
	   	if (StringUtils.isNotBlank(queryVo.getFullName())) {
	   		jpql.append(" and _channel.fullName like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getFullName()));
	   	}		
	
	   	if (StringUtils.isNotBlank(queryVo.getRemarks())) {
	   		jpql.append(" and _channel.remarks like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getRemarks()));
	   	}		
	
	
	   	if (StringUtils.isNotBlank(queryVo.getDbType())) {
	   		jpql.append(" and _channel.dbTable like ?");
	   		conditionVals.add(MessageFormat.format("%{0}%", queryVo.getDbType()));
	   	}		
	   	// koala3当前页从0开始
        Page<Channel> pages = queryChannel.createJpqlQuery(jpql.toString()).setParameters(conditionVals).setPage(currentPage-1, pageSize).pagedList();
       
        for (Channel channel : pages.getData()) {
            ChannelDTO channelDTO = new ChannelDTO();
            channelDTO.domain2Dto(channel);
            result.add(channelDTO);
        }
        
        return new Page<ChannelDTO>(pages.getPageIndex(), pages.getResultCount(), pageSize, result);
	}
}
